package com.maimang.platform.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.maimang.platform.datasource.DynamicDataSource;
import com.maimang.platform.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname DruidConfig
 * @description druid配置多数据源
 * @date 2019-04-23 18:05
 * @email jintaozhao@qq.com
 */
@Configuration
// @MapperScan(basePackages = {
// 		"com.maimang.platform.activiti.mapper",
// 		"com.maimang.platform.common.mapper",
// 		"com.maimang.platform.quartz.mapper",
// 		"com.maimang.platform.gen.mapper",
// 		"com.maimang.platform.dsp.mapper"
// }, sqlSessionFactoryRef = "sqlSessionFactory")
public class DruidConfig {

	/**
	 * 主数据库连接属性配置
	 *
	 * @param druidProperties 数据库连接属性
	 * @return
	 */
	// @Primary
	@Bean(name = "master")
	@ConfigurationProperties("spring.datasource.druid.master")
	public DataSource masterDataSource(DruidProperties druidProperties) {
		DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
		return druidProperties.dataSource(dataSource);
	}

	/**
	 * 从数据库连接属性配置
	 *
	 * @param druidProperties 数据库连接属性
	 * @return
	 */
	@Bean(name = "slave")
	@ConfigurationProperties("spring.datasource.druid.slave")
	// @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
	public DataSource slaveDataSource(DruidProperties druidProperties) {
		DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
		return druidProperties.dataSource(dataSource);
	}

	/**
	 * DSP数据库连接属性配置
	 *
	 * @param druidProperties 数据库连接属性
	 * @return
	 */
	@Bean(name = "dsp")
	@ConfigurationProperties("spring.datasource.druid.dsp")
	// @ConditionalOnProperty(prefix = "spring.datasource.druid.dsp", name = "enabled", havingValue = "true")
	public DataSource dspDataSource(DruidProperties druidProperties) {
		DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
		return druidProperties.dataSource(dataSource);
	}

	/**
	 * 多数据源配置
	 *
	 * @param masterDataSource 主数据库
	 * @param slaveDataSource  从数据库
	 * @param dspDataSource    DSP数据库
	 * @return
	 */
	@Primary
	@Bean(name = "dynamicDataSource")
	public DynamicDataSource dataSource(@Qualifier("master") DataSource masterDataSource,
	                                    @Qualifier("slave") DataSource slaveDataSource,
	                                    @Qualifier("dsp") DataSource dspDataSource) {
		Map<Object, Object> targetDataSources = new HashMap<>();
		targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
		targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
		targetDataSources.put(DataSourceType.DSP.name(), dspDataSource);
		return new DynamicDataSource(masterDataSource, targetDataSources);
	}

	/**
	 * 会话工厂
	 *
	 * @param dynamicDataSource
	 * @return
	 * @throws Exception
	 */
	// @Bean(name = "sqlSessionFactory")
	// @ConfigurationProperties(prefix = "mybatis")
	// public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
	// 	SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
	// 	bean.setDataSource(dynamicDataSource);
	// 	bean.setVfs(SpringBootVFS.class);
	// 	ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	// 	bean.setMapperLocations(resolver.getResources("classpath*:mapper/**/*Mapper.xml"));
	// 	// bean.setTypeAliasesPackage("com.maimang.platform.*.entity");
	// 	return bean.getObject();
	// }
	//
	// /**
	//  * 配置事务管理器
	//  *
	//  * @param dynamicDataSource 数据源
	//  * @return
	//  */
	// @Bean
	// public PlatformTransactionManager transactionManager(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) {
	// 	return new DataSourceTransactionManager(dynamicDataSource);
	// }
}
